package com.cloud.pole.common.util.security;

import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

public class WalletSecurity {

	// 验证签名1 userId + token + chainId +noteStr + walletPwd
	public static boolean verifySign(String userId, String token,
			String noteStr, String walletPwd, String publicKey, String sign,
			String chainId) {

		StringBuffer tempStr = new StringBuffer();
		tempStr.append(userId);
		tempStr.append(token);
		tempStr.append(chainId);
		tempStr.append(noteStr);
		tempStr.append(walletPwd);
		String md5TempStr = MD5Utils.md5Hex(tempStr.toString());
		boolean status = RSAUtils.verifySign(md5TempStr.getBytes(), publicKey,
				sign);
		return status;
	}

	// 验证签名2 userId + token + walletId + noteStr + walletPwd
	public static boolean verifySign1(String userId, String token,
			String walletId, String noteStr, String walletPwd,
			String publicKey, String sign) {

		StringBuffer tempStr = new StringBuffer();
		tempStr.append(userId);
		tempStr.append(token);
		tempStr.append(walletId);
		tempStr.append(noteStr);
		tempStr.append(walletPwd);
		String md5TempStr = MD5Utils.md5Hex(tempStr.toString());
		boolean status = RSAUtils.verifySign(md5TempStr.getBytes(), publicKey,
				sign);
		return status;
	}

	// 解密noteStr
	public static String decryptNoteStr(String noteStr, String privateKey) {
		String decodedNoteStr = null;
		try {
			byte[] decodedDataNoteStr = AESUtils.parseHexStr2Byte(noteStr);
			byte[] decodedData = RSAUtils.decryptByPrivateKey(
					decodedDataNoteStr, privateKey);
			decodedNoteStr = new String(decodedData);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return decodedNoteStr;
	}

	// 解密walletPwd
	public static String decryptWalletPwd(String noteStr, String privateKey,
			String walletPwd) {
		String decodeNotestr = null, strDecryptWallet = null;
		try {
			decodeNotestr = decryptNoteStr(noteStr, privateKey);
			strDecryptWallet = AESUtils.decode(walletPwd, decodeNotestr);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strDecryptWallet;
	}

	// 产生16位随机数
	public static String generateNoteStr() {
		String noteStr = String.valueOf(System.currentTimeMillis());
		noteStr = StringUtils.leftPad(noteStr, 16, "0");
		return noteStr;
	}

	// 公钥加密随机数
	public static String encryptNoteStrByPublicKey(String noteStr,
			String publicKey) {
		String encodedNoteStr = null;
		try {
			byte[] dataNoteStr = noteStr.getBytes();
			byte[] encodedDataNoteStr = RSAUtils.encryptByPublicKey(
					dataNoteStr, publicKey);
			encodedNoteStr = AESUtils.parseByte2HexStr(encodedDataNoteStr);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return encodedNoteStr;
	}

	// 公钥加密
	public static String encryptByPublicKey(String data, String publicKey) {
		String strEncryp = null;
		try {
			byte[] dataBytes = RSAUtils.encryptByPublicKey(data.getBytes(),
					publicKey);
			strEncryp = AESUtils.parseByte2HexStr(dataBytes);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strEncryp;
	}

	// 私钥解密
	public static String decryptByPrivateKey(String data, String privateKey) {

		return RSAUtils.decryptByPrivateKey(data, privateKey);

	}

	// 通过公钥加密的noteStr和私钥 解密字符串
	public static String decryptStrByEncodedNoteStr(String encodedNoteStr,
			String privateKey, String encodedStr) {
		String decodedNotestr = null, decodedStr = null;
		try {
			decodedNotestr = decryptNoteStr(encodedNoteStr, privateKey);
			decodedStr = AESUtils.decode(encodedStr, decodedNotestr);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return decodedStr;
	}

	// AES加密
	public static String AESEncrypt(String data, String password) {
		String encryptData = AESUtils.encode(data, password);
		return encryptData;
	}

	public static void main(String[] args) {
		String walletPwd = "123456";
		String walletPrivateKey = "0f01e";
		String mnemonic = "slender prefer dinosaur quarter dream label avocado spawn embody client have assume";
		/*
		 * String encodedMnemonic=AESEncrypt(mnemonic,noteStr);
		 * System.out.println(encodedMnemonic);
		 */
		String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKCdQpUIQRGCW05Yzw3gcHuzNKHpsURE3IE552/fXeVgoQhPMJDLuIrnFlAjllLmpIPSO4556CJmP/UUodF1trOCI5W59iRbhSbfYCvKaOdHF7KJIb6dyOZMxtArCPl5XrBV7qmo0bv/EKvLKK2etFD5+RIlSsDBfLtEg0SZs82LAgMBAAECgYBEIrKOOpQv81b8o5wizOWq9FkNnu++gF/yyjeuM9nHZvipZ764jlPZSTMkCBTLUmDUMylkxb5+aVYUiA4lp4Q7ckOXw7h1wUai+KD9ogbJu49IAniYwwM+t19KKkhawCbyqK+bjWlJ0tqSfKqhx2zxbyTXdWyN9WMuSyorGuKRsQJBANgXXTyIwmgxtM2fVl5DNiGKi6UeKcfowP+dmvWBGxD8nI9PPTPMXfovNqkjZrFFwyyN/sT6Fyt47kmbx9C5Cc8CQQC+RvxU1iMnkYFJpV+DpGChBVtSuZyS4NkXLwzKQMrKREEeibo8ljc3IlfNpZomCSqO4AxWwdDP/wP3h6X9gTuFAkAXFzAMVuTHM4CJdFxLSE1Rejd/1mBKj1GIPvQfBcO5pfSoz6PPlCf6ou7pVfIUQUsgyK+PptWw/IQh83Z/lDY7AkEAny6trJ+Uu2aMHbt+45ehyzO7CcMETRBULYQMFCwOOxbMz6pzJnqY6MQPZpiTFBz5AJxJVk9AzLFQdUWDRUhXLQJAdLu6x5bfoVbNI1wtFR3aMdWiBTjAPyedibUmaKQQXsl5ZKHmDCAFWqRGnpNCI14WvNwD7N84vp4ciWJvadX+RQ==";
		String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRTcTZ10GvLZfDXOaZKR+P63rRCn+StPqOKLnOpvGtyCigGvvdEbWdC51JFIkIYV728UaZpWcjru4JbWq7kEdRrMjfEuWSNllY4OmcScSNI9mMqtqP/TojzPrbEauP6DmbIbmCKnMDqpd+noPPe1JxjJg2j5HC2LX+r3wLRiwrLQIDAQAB";
		String noteStr = "0001532667514555";
		String encodedNoteStr = WalletSecurity.encryptByPublicKey(noteStr, publicKey);
		System.out.println("加密noteStr：" + encodedNoteStr);
		String encodedWalletPwd = WalletSecurity.AESEncrypt(walletPwd, noteStr);
		walletPrivateKey = WalletSecurity.AESEncrypt(walletPrivateKey, noteStr);
		System.out.println("加密密码：" + encodedWalletPwd);
		System.out.println("加密私钥：" + walletPrivateKey);

	
	}

}
